﻿using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 后整理管理系统.出库管理;
using 纺织贸易管理系统.选择窗体;

namespace 纺织贸易管理系统.新增窗体
{
    public partial class 架子出库 : Sunny.UI.UIForm 
    {
        public 架子出库()
        {
            InitializeComponent();
            uiDatePicker1.Value = DateTime.Now.Date;
            导入在库的子架号();
        }

        private void 确认出库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrWhiteSpace ( txtkehu.Text))
            {
                Sunny.UI.UIMessageBox.ShowError("请选择客户。\r\n客户不能为空");
                return;
            }
            try
            {
                var stocks = new List<StockTable>();
                foreach (string j in uiTransfer1.ItemsRight)
                {
                    string[] jiazi = j.Split('-');
                    if (jiazi[1] == "自己公司")
                    {
                        Connect.dbcontext.Updateable<JiaziTable>().SetColumns(x => new JiaziTable() { State = txtkehu.Text }).Where(X => X.JiaziID == jiazi[0] && X.Guisuo == jiazi[1]).ExecuteCommand();
                    }
                    else
                    {
                        Connect.dbcontext.Deleteable<JiaziTable>().Where(X => X.JiaziID == jiazi[0] && X.Guisuo == jiazi[1]).ExecuteCommand();
                    }
                    ZhijiaMingxiTableService.InsertZhijiaMingxiTable(new ZhijiaMingxiTable()
                    {
                        Danhao = "CK" + DateTime.Now.ToString(),
                        Bianhao = JiaziTableService.GetOneJiaziTable(X => X.JiaziID == jiazi[0] && X.Guisuo == jiazi[1]).ID.ToString(),
                        _date = uiDatePicker1.Value,
                        Remarker = txtbeizhu.Text,
                        Leixing = "出库",
                        Kehu = txtkehu.Text
                    });
                    stocks.AddRange(Connect.dbcontext.Queryable<StockTable>().Where(x => x.Kuwei == jiazi[0]).ToList());
                }
                var fm = new 架子出库确认() { stocks=stocks};
                fm.ShowDialog();
                导入在库的子架号();
                txtbeizhu.Text = "";
                txtkehu.Text = "";
            }
            catch(Exception ex)
            {
                ShowMessageBox.ShowErrorBox("出库的时候发送了错误"+ex.Message);
            }
        }
        private void 导入在库的子架号()
        {
            uiTransfer1.ItemsLeft.Clear();
            uiTransfer1.ItemsRight.Clear();
            foreach (var n in JiaziTableService.GetJiaziTablelst(x => x.State == "自己公司"))
            {
                uiTransfer1.ItemsLeft.Add(n.JiaziID + "-" + n.Guisuo);
            }
        }

        private void txtkehu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var fm = new 客户选择();
            fm.ShowDialog();
            txtkehu.Text = fm.linkman.MC;
        }
    }
}
